package com.example.jdai.jdai.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.rest.RestApi;
import com.example.jdai.jdai.threeparty.FileUploadThread;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataConnect;
import com.example.jdai.jdai.util.DataLnfo;
import com.example.jdai.jdai.util.Tools;

import java.util.Map;

import butterknife.BindArray;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

public class ConnectActivity extends Activity implements BackRest.DoLong{

    @BindView(R.id.tb)
    TopBar tb;
    @BindView(R.id.ll_my_connect_to)
    LinearLayout llMyConnectTo;
    @BindView(R.id.ll_my_connect_warn)
    LinearLayout llMyConnectWarn;
    @BindView(R.id.ll_other_connect_to)
    LinearLayout llOtherConnectTo;
    @BindView(R.id.ll_other_connect_warn)
    LinearLayout llOtherConnectWarn;
    @BindView(R.id.activity_connect)
    LinearLayout activityConnect;
    @BindView(R.id.sv_content)
    ScrollView svContent;
    @BindArray(R.array.text_connect_to_all)
    String[] textConnectToAll;
    @BindArray(R.array.text_connect_other_all)
    String[] textConnectOtherAll;
    final int MY_TO = 0;
    final int MY_OTHER = 1;
    @BindView(R.id.tv_to)
    TextView tvTo;
    @BindView(R.id.tv_to_tel)
    TextView tvToTel;
    @BindView(R.id.tv_other)
    TextView tvOther;
    @BindView(R.id.tv_other_tel)
    TextView tvOtherTel;
    @BindString(R.string.no_complete)
    String onComplete;
    private ProgressDialog pd;
    @BindString(R.string.http_tip)
    String httpTip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect);
        ButterKnife.bind(this);
        pd=new ProgressDialog(this);
        pd.setMessage(httpTip);
        pd.setCanceledOnTouchOutside(false);
        initData();
        tb.setTopBarLisener(new TopBar.TopBarLisener() {
            @Override
            public void back() {
                DataAll.dataConnect.saveAll();
                finish();
            }

            @Override
            public void more() {
                //DataAll.dataConnect.saveAll();
                if (checkInput())
                {
                    pd.show();
                    DataAll.dataConnect.data.put(DataConnect.CT_STATUS,"");
                    saveAll();
                    MyApplication.backRest.ConnectOk(ConnectActivity.this);

                }
                else {
                    Toast.makeText(ConnectActivity.this,onComplete,Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @OnClick({R.id.ll_my_connect_to, R.id.ll_my_connect_warn, R.id.ll_other_connect_to, R.id.ll_other_connect_warn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_my_connect_to:
                new MyConnectPopuWindows(this, textConnectToAll
                        ,DataAll.dataConnect.data.get(DataConnect.CT_TO))
                        .setBtOk(new MyConnectPopuWindows.MySetOk() {
                    @Override
                    public void onOkclick(int index, String itemname) {
                        DataAll.dataConnect.data.put(DataConnect.CT_TO,itemname);
                       // DataAll.dataConnect.data.put(DataConnect.CT_TO_INDEX,index+"");
                        tvTo.setText(itemname);
                    }
                }).showAtLocation(svContent, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.ll_other_connect_to:
                new MyConnectPopuWindows(this, textConnectOtherAll
                        ,DataAll.dataConnect.data.get(DataConnect.CT_OTHER))
                        .setBtOk(new MyConnectPopuWindows.MySetOk() {
                    @Override
                    public void onOkclick(int index, String itemname) {
                        DataAll.dataConnect.data.put(DataConnect.CT_OTHER,itemname);
                        //DataAll.dataConnect.data.put(DataConnect.CT_OTHER_INDEX,index+"");
                        tvOther.setText(itemname);
                    }
                }).showAtLocation(svContent, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.ll_my_connect_warn:
                startGetConnect(MY_TO);
                break;
            case R.id.ll_other_connect_warn:
                startGetConnect(MY_OTHER);
                break;
        }
    }
    @OnLongClick({R.id.ll_my_connect_to, R.id.ll_my_connect_warn, R.id.ll_other_connect_to, R.id.ll_other_connect_warn})
    public boolean OnLongClick(View view)
    {
        switch (view.getId()) {
            case R.id.ll_my_connect_to:
                DataAll.dataConnect.data.put(DataConnect.CT_TO,"");
                //DataAll.dataConnect.data.put(DataConnect.CT_TO_INDEX,"0");
                tvTo.setText("");
                break;
            case R.id.ll_other_connect_to:
                DataAll.dataConnect.data.put(DataConnect.CT_OTHER,"");
                //DataAll.dataConnect.data.put(DataConnect.CT_OTHER_INDEX,"0");
                tvOther.setText("");
                break;
            case R.id.ll_my_connect_warn:
                DataAll.dataConnect.data.put(DataConnect.CT_TO_TEL,"");
                tvToTel.setText("");
                break;
            case R.id.ll_other_connect_warn:
                DataAll.dataConnect.data.put(DataConnect.CT_OTHER_TEL,"");
                tvOtherTel.setText("");
                break;

        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK) {
            String tel= Tools.getConnectByUri(data.getData());
            switch (requestCode) {
                case MY_TO:
                    tvToTel.setText(tel);
                    DataAll.dataConnect.data.put(DataConnect.CT_TO_TEL,tel);
                    break;
                case MY_OTHER:
                    tvOtherTel.setText(tel);
                    DataAll.dataConnect.data.put(DataConnect.CT_OTHER_TEL,tel);
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DataAll.dataConnect.saveAll();
    }
void saveAll(){
    DataAll.dataConnect.data.put(DataConnect.CT_TO,tvTo.getText().toString().trim());
    DataAll.dataConnect.data.put(DataConnect.CT_TO_TEL,tvToTel.getText().toString().trim());
    DataAll.dataConnect.data.put(DataConnect.CT_OTHER,tvOther.getText().toString().trim());
    DataAll.dataConnect.data.put(DataConnect.CT_OTHER_TEL,tvOtherTel.getText().toString().trim());
    DataAll.dataConnect.saveAll();

}
    void initData()
    {
        //DataAll.dataConnect=new DataConnect();
        tvTo.setText(DataAll.dataConnect.data.get(DataConnect.CT_TO));
        tvToTel.setText(DataAll.dataConnect.data.get(DataConnect.CT_TO_TEL));
        tvOther.setText(DataAll.dataConnect.data.get(DataConnect.CT_OTHER));
        tvOtherTel.setText(DataAll.dataConnect.data.get(DataConnect.CT_OTHER_TEL));
    }
    void startGetConnect(int id) {
        startActivityForResult(new Intent(Intent.ACTION_PICK,
                ContactsContract.Contacts.CONTENT_URI), id);
    }
    boolean checkInput(){
        if(TextUtils.isEmpty(tvTo.getText().toString().trim())||
                TextUtils.isEmpty(tvToTel.getText().toString().trim())||
                TextUtils.isEmpty(tvOther.getText().toString().trim())||
                TextUtils.isEmpty(tvOtherTel.getText().toString().trim())
                )
            return false;
        return true;
    }

    @Override
    public void doDone(int mark,Map str) {
        switch (mark){
            case 0:
                pd.dismiss();
                //startActivity(new Intent(ConnectActivity.this,ZmWebActivity.class));
                MyApplication.startNext(ConnectActivity.this);
                finish();
                break;
            case 1:
                break;
            case 2:
                pd.dismiss();
                Toast.makeText(MyApplication.applicationContext,"出现错误",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
